﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using System.IO;
using OpenPop.Pop3;
using OpenPop.Mime;
using System.Data;
using System.Threading;
using System.Globalization;
using System.Collections;
public partial class Mails : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
       
      
        if (!Page.IsPostBack)
        {
            //if (Request.QueryString["mail"] != null)
            //{
            //    norms.Visible = false;
            //    TextBox2.Text = Request.QueryString["mail"];

            //}
            //else
            //{
              //  customers.DataSource = SqlDataSource1;
                customers.DataTextField = "company";
                //customers.DataValueField = "email1";
                customers.DataBind();
           // }
           
        }
       
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);


            }
        }

    }
    public void sendwithattach(string too, string subjects)
    {
        EO.Web.AJAXPostedFile[] files = AJAXUploader1.PostedFiles;
        string[] names = new string[files.Length];
        for (int i = 0; i < files.Length; i++)
        {


            names[i] = files[i].TempFileName + "," + files[i].FullClientFileName;//System.IO.Path.GetFileName(files[i].TempFileName); ////Server.MapPath("~/temp/Example.xml") + 
            // Server.MapPath("~\temp\\") + files[i].TempFileName;

        }
        DB.sendingmail(too, subjects, Editor1.Content, names);
        AJAXUploader1.ClearPostedFiles();


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (TextBox2.Text.Contains(','))
        {
            string[] mails = TextBox2.Text.Split(',');
            foreach (string dd in mails)
            {
                if (AJAXUploader1.PostedFiles.Length > 0)
                {


                    sendwithattach(dd, subject.Text);
                }
                //if (AsyncFileUpload1.HasFile)
                //{
                //    DB.sendingmail(dd, subject.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);

                //}
                else
                {
                    DB.sendingmail(dd, subject.Text, Editor1.Content); //TextBox1.Text);
                   
                }
               
            }
        }
        else
        {
            if (AJAXUploader1.PostedFiles.Length > 0)
            {
             //   DB.sendingmail(TextBox2.Text, subject.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);
                sendwithattach(TextBox2.Text, subject.Text);

            }
            else
            {
                DB.sendingmail(TextBox2.Text, subject.Text, Editor1.Content);
            }
        
        }
        if (DropDownList1.SelectedValue == "companies")
        {
            foreach (ListItem item in customers.Items)
            {
                if (item.Selected)
                {
                    List<string> fields = new List<string>();
                    ArrayList values = new ArrayList();
                    List<string> types = new List<string>();
                    fields.Add("dates");
                    values.Add(DateTime.Now);
                    types.Add("datetime");
                    //////////////////
                    fields.Add("checked");
                    values.Add(0);
                    types.Add("int");
                    ///////////////////////
                    fields.Add("message");
                    values.Add(Editor1.Content);
                    types.Add("string");
                    //////////////////////////
                    fields.Add("title");
                    values.Add(subject.Text);
                    types.Add("string");
                    //////////////
                    fields.Add("company_id");
                    values.Add(item.Value.Split(',')[0]);
                    types.Add("string");

                    DB.INSERT("system_messages", fields, types, values);
                }
            }
        }
        //if (Session["filter"] != null)
        //{
        //    Session.Remove("filter");
        //}
        
        Page.ClientScript.RegisterStartupScript(this.GetType(), "reports", " Sents();", true);

    }
   

    
    protected void Link1_Click(object sender, EventArgs e)
    {

        string dd = ((LinkButton)sender).ToolTip;
     
    }
    
    

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    
    {
        TextBox2.Text = "";

        foreach (ListItem x in customers.Items)
        {
            if (x.Selected) 
                TextBox2.Text += x.Value.Split(',')[1] + ",";
       
        }
        TextBox2.Text = TextBox2.Text.Trim(",".ToCharArray()[0]);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        foreach (ListItem x in customers.Items)
        {
            x.Selected = true;

        }
        TextBox2.Text = "";
        //if (Session["filter"] != null)
        //{

        //    SqlDataSource1.SelectCommand = (string)Session["filter"];
        //    customers.DataBind();
        //}
        foreach (ListItem x in customers.Items)
        {
           TextBox2.Text += x.Value.Split(',')[1] + ",";

        }
        TextBox2.Text = TextBox2.Text.Trim(",".ToCharArray()[0]);
    }


    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {


        HttpFileCollection postedfile = Request.Files;
        if (postedfile[0].ContentLength > 5196000)
        {
         
            return; 

        }
       
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "companies")
        {
            SqlDataSource1.SelectCommand = "SELECT company, cast(id as nvarchar) +','+ email1 as email1 from " + DropDownList1.SelectedValue;
            customers.DataTextField = "company";
        }
        else
        {
            string tex = DropDownList1.SelectedValue.TrimEnd('s');
            SqlDataSource1.SelectCommand = "SELECT " + tex + ",email1 FROM " + DropDownList1.SelectedValue;
            customers.DataTextField = tex;

        }
        // customers.DataSource=SqlDataSource1;
        //customers.DataValueField="email1";
        customers.DataBind();
    }
}
